<?php

class GmToolsTestAction extends SzAbstractAction
{
    protected $paramTypes = array();

    public function execute()
    {
        set_time_limit(0);

        // 获取数据
        $header = array(
            "version" => $_SERVER['HTTP_VERSION'],# 消息体版本
            "patch-version" => $_SERVER['HTTP_PATCH_VERSION'],# 游戏版本
            "secret-key" => $_SERVER['HTTP_SECRET_KEY'],# 密钥文
        );
        $jsonBody = trim(file_get_contents("php://input"));

        // check secret-key
        if (md5($jsonBody . "EDFRbcddy73b9dgvajdkj3d") !== $header['secret-key']) {
            SzLogger::get()->debug("GmToolsTestAction --------------" . var_export($jsonBody, true));
            // 自定义 err_code err_msg
//            throw new SzException(25001);
        }
        // dispatch code
        $body = json_decode($jsonBody, true);
        $cmdCode = $body['code'];
        SzLogger::get()->debug("-----code-------" . var_export($cmdCode, true) . "--------------");
        SzLogger::get()->debug("-----body-------" . $jsonBody . "--------------");
        $_res = null;
        switch ($cmdCode) {
            case 0x01 :
                if ($body['op'] == 'edit') {
                    $_res = GmToolsService::get()->changeUserInfo($body['data']['search'], $body['data']['replace']);
                } else if ($body['op'] == 'query') {
                    $_res = GmToolsService::get()->getUserInfo($body['data']['search']);
                }
                break;
            case 0x02 :
                $_res = GmToolsService::get()->getUserDetailedInfo($body['data']['search']);
                break;
            case 0x04:
                if ($body['op'] == 'edit') {
                    $_res = GmToolsService::get()->changeNotice($body['data']['params']);
                } else {
                    switch ($body['data']['action']) {
                        case 'send':
                            $_res = GmToolsService::get()->setNotice($body['data']['params']);
                            break;
                        case 'cancel':
                            $_res = GmToolsService::get()->deleteNotice($body['data']['params']);
                            break;
                        case 'intro':
                            $_res = GmToolsService::get()->getNotice($body['data']['params']);
                            break;
                    }
                }
                break;
            case 0x07:
                if ($body['op'] == 'edit') {
                    $_res = GmToolsGiftCodeService::get()->changeBatchGiftCodeInfo($body['data']['params']);
                } else {
                    switch ($body['data']['action']) {
                        case 'send':
                            $_res = GmToolsGiftCodeService::get()->createBatchGiftCode($body['data']['params']);
                            break;
                        case 'intro':
                            $_res = GmToolsGiftCodeService::get()->getBatchGiftCode($body['data']['params']);
                            break;
                    }
                }
                break;
            case 0x08:
//                {"code": 8,"data": {"action": "", "params": {"orderIds": ["ZFSQA120170920c2e1a96e9db28832","ZFSQA120170920c2e1a96e9db28832"]}},"op": "query"}
                if ($body['op'] == 'query') {
                    $_res = GmToolsService::get()->selectUserInfoByOrder($body['data']['params']);
//                else {
//                        switch ($body['data']['action']) {
//                            case 'select':
//                                $_res = GmToolsGiftCodeService::get()->createBatchGiftCode($body['data']['params']);
//                                break;
//                        }
//                    }
                }
                break;
            case 0x09:
                if ($body['op'] == 'query') {
//                 {"code": 9,"data": {"action": "", "params": {}},"op": "query"}
                    $_res = GameService::get()->serverStatus();
                } else if ($body['op'] == 'close') {
//                 {"code": 9,"data": {"action": "", "params": {"msg":"xxxxxxxxxxx"}},"op": "close"}
                    $_res = GameService::get()->closeServer($body['data']['params']);
                } else {
                    $_res = GameService::get()->openServer();
                }
                SzLogger::get()->debug("----------------------".$body['op']."------------------------");
                break;
            case 0x0a:
                if ($body['op'] == 'add') {
//                {"code": 10,"data": {"action": "", "params": {"white_list": ["51","52","53"]}},"op": "add"}
                    $_res = GameService::get()->addWriteList($body['data']['params']);
                } else if ($body['op'] == 'delete') {
//                {"code": 10,"data": {"action": "", "params": {"white_list": ["51","52","53"]}},"op": "delete"}
                    $_res = GameService::get()->deleteWriteList($body['data']['params']);
                } else if ($body['op'] == 'delete_all') {
//                {"code": 10,"data": {"action": "", "params": {}},"op": "delete_all"}
                    $_res = GameService::get()->clearWriteList();
                } else {
//                {"code": 10,"data": {"action": "", "params": {"white_list": ["51","52","53"]}},"op": "query"}
                    $_res = GameService::get()->queryWriteList();
                }
                break;
            case 0x0b:
                if ($body['op'] == 'get') {
                    $_res = ActivityService::get()->getActivityLibs(true);
                } else if ($body['op'] == 'update') {
                    $_res = ActivityService::get()->updateActivityLib($body['data']['params']);
                }
                break;
            case 0x0c:
                if ($body['op'] == 'query') {
                    $_res = GmToolsService::get()->getUserLive($body['data']['params']);
                }
                break;
            default:
                throw new SzException(25002);
        }

        $response = array(
            "status" => "1",
            "err_msg" => "",
            "op" => $body['op'],
            "content" => $_res,
            "header" => $header,
        );
        return $this->buildResponse(
            $response
        );
    }
}